package common.store.web.service.impl;

import common.store.web.bean.Product;
import common.store.web.bean.User;
import common.store.web.dao.ProductDao;
import common.store.web.service.ProductService;
import common.store.web.vo.PageVo;

import java.util.List;

public class ProductServiceImpl implements ProductService {

    private ProductDao productDao;

    public ProductServiceImpl(){
        productDao = new ProductDao();
    }


    @Override
    public PageVo getPageVoByCurrentPageAndMaxCount(Integer currentPage, Integer maxCount, String cid) {

        PageVo pageVo = new PageVo();
        pageVo.setCurrentPage(currentPage);
        pageVo.setCid(cid);
        //获取数据库商品数量 / maxCount  需要向上取整 Math.ceil() 天花板
        Integer productCount = productDao.getProductCountByCid(cid);
        //计算总页数
        pageVo.setTotalPages((int)Math.ceil(productCount * 1.0/ maxCount));

        List<Product> productList = productDao.getProductListByCurrentPageAndMaxCountAndCid(currentPage , maxCount , cid);
        pageVo.setProductList(productList);
        return pageVo;
    }

    @Override
    public Product getProductByPid(String pid) {
        return productDao.getProductByPid( pid);
    }

    @Override
    public List<Product> getHotProduct() {
        return productDao.getHotProduct();
    }

    @Override
    public List<Product> getProductsByWord(String word) {
        return productDao.getProductListByWord(word);
    }

    @Override
    public Integer addCartByPid(Product product) { return productDao.addCartByPid(product); }

    @Override
    public List<Product> getProductsByuser(String uid) { return productDao.getProductsByuser(uid); }
}
